/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 combat_header_table.cpp

	$Header: /heroes4/combat_header_table.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "combat_header_table.h"
#include "string_vector.h"
#include "format_string.h"

char const t_resource_traits<t_combat_header_table>::prefix[] = "combat_header_table_cache";

char const t_resource_traits<t_combat_header_table>::extension[] = "dat";

// ------------------------------------------------------------------
// combat header consts
// ------------------------------------------------------------------

const std::string k_combat_header_table_name("combat_header");

const std::string k_combat_header_table_resource_name(
													  format_string("%s.%s.h4d",  
													  t_resource_traits<t_combat_header_table>::prefix,
													  k_combat_header_table_name.c_str()));
// Constructors / Destructor
t_combat_header_table::t_combat_header_table()  {}
t_combat_header_table::~t_combat_header_table() {}

// Member functions

// ------------------------------------------------------------------------------
// specificly made for combat_object_placer, fill the obstacle array with its
// cached combat_object_model_base data.
// ------------------------------------------------------------------------------
void t_combat_header_table::fill_obstacle_array(t_obstacle_data_array & array) const
{
	//PDL::
	//-fill the array

	t_obstacle_data_array::const_iterator index_iter;
	
	for ( index_iter = m_table.begin(); index_iter != m_table.end(); ++index_iter )
	{
		t_combat_header_obstacle_data		entry;
		
		entry.name		= index_iter->name;
		entry.obstacle	= index_iter->obstacle;
		
		array.push_back( entry );
	}
	
	return;
}

// ------------------------------------------------------------------------------
// create a string for the date (mm/dd/yy)
// ------------------------------------------------------------------------------
void t_combat_header_table::get_date_string(std::string & date) const
{
	t_int32 amercian_hour  = m_hour-12;
	t_int32 amercian_suffix= m_hour/12;

	if (amercian_suffix == 0)
	{
		if (m_minute < 10)
			date = format_string("%i/%i/%i   %i:0%i AM\0", m_month, m_day, m_year, m_hour, m_minute);
		else
			date = format_string("%i/%i/%i   %i:%i AM\0", m_month, m_day, m_year, m_hour, m_minute);
	}
	else
	{
		if (m_minute < 10)
			date = format_string("%i/%i/%i   %i:0%i PM\0", m_month, m_day, m_year, amercian_hour, m_minute);
		else
			date = format_string("%i/%i/%i   %i:%i PM\0", m_month, m_day, m_year, amercian_hour, m_minute);
	}
	
	return;
}

// ------------------------------------------------------------------------------
// insert the given combat_object_base data into the table
// ------------------------------------------------------------------------------
void t_combat_header_table::insert( std::string const& name , t_combat_object_model_base const& model_data)
{
	//PDL::
	//-insert into the table by creating a new t_combat_header_entry, since the tables data is 
	//	independent from the source
	
	t_combat_header_obstacle_data					entry;
	
	entry.name		= name;
	entry.obstacle	= model_data;

	m_table.push_back( entry );

	return;
}

// ------------------------------------------------------------------------------
// read in from a a stream
// ------------------------------------------------------------------------------
bool t_combat_header_table::read( std::streambuf& buffer )
{
	//PDL::
	//-read in the date data
	//-read in number of entries
	//-for every entry in the table read in the name, then combat data using their
	//	respective read functions

	try
	{
		m_month		= get< t_uint32 >( buffer );
		m_day		= get< t_uint32 >( buffer );
		m_year		= get< t_uint32 >( buffer );
		m_hour		= get< t_uint32 >( buffer );
		m_minute	= get< t_uint32 >( buffer );
		
		t_uint32 size = get< t_uint32 >( buffer );

		m_table.resize( size );

		t_obstacle_data_array::iterator index_iter;

		for ( index_iter = m_table.begin(); index_iter != m_table.end(); ++index_iter )
		{
			read_string(buffer,index_iter->name);
			index_iter->obstacle.read(buffer);
		}
	}
	catch ( t_streambuf_read_failure const & )
	{
		return false;
	}

	return true;
}

// ------------------------------------------------------------------------------
// read in a string from a a stream
// ------------------------------------------------------------------------------
void t_combat_header_table::read_string(std::streambuf& buffer , std::string & string_in )
{
	t_uint32 size = get< t_uint32 >( buffer );

	string_in.resize( size );

	std::string::iterator str_iter;
	
	for ( str_iter = string_in.begin(); str_iter != string_in.end(); ++str_iter )
	{
		*str_iter = get<std::string::value_type> (buffer);
	}

	return;
}

// ------------------------------------------------------------------------------
// store the date in numeric format
// ------------------------------------------------------------------------------
void t_combat_header_table::set_date(t_int32 month , t_int32 day, t_int32 year, t_int32 hour, t_int32 minute )
{
	m_month		= month;
	m_day		= day;
	m_year		= year;
	m_hour		= hour;
	m_minute	= minute;
}

// ------------------------------------------------------------------------------
// get the size of the table
// ------------------------------------------------------------------------------
t_combat_header_table::t_size_type t_combat_header_table::size() const
{
	return m_table.size();
}

// ------------------------------------------------------------------------------
// write out into a stream
// ------------------------------------------------------------------------------
bool t_combat_header_table::write( std::streambuf& buffer ) const
{
	//PDL::
	//-write out the date data
	//-write out number of entries.
	//-for every entry in the table write out the name, then combat data using their
	//	respective write functions
	
	try
	{
		put< t_uint32 >( buffer, m_month	);
		put< t_uint32 >( buffer, m_day		);
		put< t_uint32 >( buffer, m_year		);
		put< t_uint32 >( buffer, m_hour		);
		put< t_uint32 >( buffer, m_minute	);

		put< t_uint32 >( buffer, m_table.size() );

		t_obstacle_data_array::const_iterator index_iter;

		for ( index_iter = m_table.begin(); index_iter != m_table.end(); ++index_iter )
		{
			write_string(buffer,index_iter->name);
			index_iter->obstacle.write(buffer);
		}
	}
	catch ( t_streambuf_read_failure const & )
	{
		return false;
	}

	return true;
}

// ------------------------------------------------------------------------------
// write out a string into a stream
// ------------------------------------------------------------------------------
void t_combat_header_table::write_string( std::streambuf& buffer , std::string const& string_out) const
{
	put< t_uint32 >( buffer, string_out.size() );
	
	std::string::const_iterator str_iter;

	for ( str_iter = string_out.begin(); str_iter != string_out.end(); ++str_iter )
	{
		put<std::string::value_type> (buffer, *str_iter);
	}

	return;
}
